<?php

/**
 * HTML based view with PHP templates
 *
 * @package Rapide_View
 *
 */
class Rapide_View_Html implements Rapide_View_Interface
{
	/**
	 * Attribute for CSS
	 *
	 */
	const CSS_ATTRIBUTE = 'css';

	/**
	 * Attribute for JavaScript
	 *
	 */
	const JS_ATTRIBUTE = 'js';

	private
		$_aHelpers = array(),
		$_aParameters = array();

	protected $_sTemplate;

	/**
	 * Call the helper
	 *
	 * @param string $sHelper helper's name
	 * @param array $aArguments arguments for helper
	 * @return unknown
	 */
	public function __call($sHelper, array $aArguments)
	{
		$sHelper = ucfirst($sHelper);

		if(!isset($this->_aHelpers[$sHelper]))
		{
			$sHelperFile = _DIR_VIEWS_HELPERS . '/' . $sHelper . '.class.php';

			$oHelper = null;

			/**
			 *	Try to get Helper from user's directory and if it does not exist,
			 *	try to get Helper from Rapide.
			 */

			if(is_readable($sHelperFile))
			{
				require_once $sHelperFile;

				$sHelperClass = 'View_Helper_' . $sHelper;

				if(!class_exists($sHelperClass))
				{
					$sError = sprintf('Helper class in helper %s does not exist', $sHelper);
					throw new Rapide_View_Html_Exception($sError);
				}

				$oHelper = new $sHelperClass($this);
			}
			else
			{
				$sRapideHelperClass = 'Rapide_View_Helper_' . $sHelper;
				$oHelper = new $sRapideHelperClass($this);
			}

			if(!method_exists($oHelper, 'execute'))
			{
				$sError = sprintf('Helper %s does not have method execute()', $sHelper);

				throw new Rapide_View_Html_Exception($sError);
			}

			$this->_aHelpers[$sHelper] = $oHelper;
		}

		return call_user_func_array(array($this->_aHelpers[$sHelper], 'execute'), $aArguments);
	}

	/**
	 * Set template
	 * 
	 * Note, that template's name must be without .php extension. Example:
	 * <code>$oView->setTemplate('News')</code>
	 *
	 * @param string $sTemplate
	 */
	public function setTemplate($sTemplate)
	{
		$this->_sTemplate = $sTemplate;
	}

	/**
	 * Render and return the content of view
	 *
	 * @return string content
	 */
	public function fetch()
	{
		if(!isset($this->_sTemplate))
		{
			$sError = 'Template has not been set';
			throw new Rapide_View_Html_Exception($sError);
		}

		ob_start();

		require $this->_getTemplateFile($this->_sTemplate);

		$sContents = ob_get_clean();

		return $sContents;
	}

	/**
	 * Render and display the content of view
	 *
	 */
	public function display()
	{
		echo $this->fetch();
	}

	protected function _getTemplateFile($sTemplate)
	{
		$sTemplateFile = _DIR_VIEWS_HTML_TEMPLATES . '/' . $sTemplate . '.php';

		if(!is_readable($sTemplateFile))
		{
			$sError = sprintf('Template %s does not exist', $sTemplate);
			throw new Rapide_View_Html_Exception($sError);
		}

		return $sTemplateFile;
	}

	/**
	 * Get a value
	 *
	 * @param string $sParameter
	 * @return mixed value
	 */
	public function __get($sParameter)
	{
		return isset($this->_aParameters[$sParameter]) ? $this->_aParameters[$sParameter] : null;
	}

	/**
	 * Set a value
	 *
	 * @param string $sParameter
	 * @param mixed $mValue
	 */
	public function __set($sParameter, $mValue)
	{
		$this->_aParameters[$sParameter] = $mValue;
	}

	/**
	 * Check if value is set
	 *
	 * @param string $sParameter
	 * @return bool
	 */
	public function __isset($sParameter)
	{
		return isset($this->_aParameters[$sParameter]);
	}
}

?>